-
Notifications
You must be signed in to change notification settings - Fork 764
Automate user curation #6590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automate user curation #6590
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,27 @@ | ||||||
from datetime import timedelta | ||||||
|
||||||
from django.conf import settings | ||||||
from django.contrib.auth import get_user_model | ||||||
from django.core.management.base import BaseCommand | ||||||
from django.utils import timezone | ||||||
|
||||||
from kitsune.users.utils import delete_user_pipeline | ||||||
|
||||||
|
||||||
class Command(BaseCommand): | ||||||
help = "Delete users who haven't logged in for more than settings.USER_EXPIRATION_DAYS days" | ||||||
|
||||||
def handle(self, *args, **options): | ||||||
User = get_user_model() | ||||||
expiration_date = timezone.now() - timedelta(days=settings.USER_EXPIRATION_DAYS) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. timezone.now() returns a datetime object. Maybe we should convert this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explain further why this should be changed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No it does not, it's totally valid. The reason for this nit was the naming of the variable. By converting to date() it just removes the time component which as you said it's not relevant for this kind of functionality. This is just nitpicking so treat it as such |
||||||
|
||||||
expired_users = User.objects.filter(last_login__lt=expiration_date) | ||||||
self.stdout.write(f"Found {expired_users.count()} expired users") | ||||||
|
||||||
for user in expired_users: | ||||||
delete_user_pipeline(user) | ||||||
self.stdout.write(f"Deleted user {user.username}") | ||||||
|
||||||
self.stdout.write( | ||||||
self.style.SUCCESS(f"Successfully processed {expired_users.count()} expired users") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The final success message calls expired_users.count() after users have been deleted, which may result in an incorrect count (likely 0). Consider storing the initial count before processing the deletions and using that stored value in the success message.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -399,6 +399,22 @@ def job_cleanup_old_account_events(): | |
call_command("cleanup_old_account_events") | ||
|
||
|
||
@scheduled_job( | ||
"cron", | ||
month="*", | ||
day="*", | ||
hour="03", | ||
minute="00", | ||
day_of_week=0, | ||
max_instances=1, | ||
coalesce=True, | ||
skip=settings.READ_ONLY, | ||
) | ||
@babis.decorator(ping_after=settings.DMS_CLEANUP_EXPIRED_USERS) | ||
def job_cleanup_expired_users(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a waffle flag/switch to control when this is enabled through admin. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. |
||
call_command("cleanup_expired_users") | ||
|
||
|
||
def run(): | ||
try: | ||
schedule.start() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
USER_INACTIVITY_DAYS
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More descriptive - done!